package com.platform.toutiao.error;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.util.json.GsonHelper;

public class TouTiaoError {
	/**
	 * 错误代码.
	 */
	private int errcode;

	/**
	 * 错误信息. （如果可以翻译为中文，就为中文）
	 */
	private String errmsg;

	private String json;

	public static TouTiaoError fromJson(String jsonStr) {
		TouTiaoError ttError = new TouTiaoError();
		Gson g = new Gson();
		JsonObject jsonObject = g.fromJson(jsonStr, JsonObject.class);
		ttError.setErrcode(GsonHelper.getInteger(jsonObject, "errcode"));
		ttError.setErrmsg(GsonHelper.getString(jsonObject, "errmsg"));
		ttError.setJson(jsonStr);
		return ttError;
	}

	@Override
	public String toString() {
		if (this.json == null) {
			return "错误代码：" + this.errcode + ", 错误信息：" + this.errmsg;
		}
		return "错误代码：" + this.errcode + ", 错误信息：" + this.errmsg + "，头条原始报文：" + this.json;
	}

	public int getErrcode() {
		return errcode;
	}

	public void setErrcode(int errcode) {
		this.errcode = errcode;
	}

	public String getErrmsg() {
		return errmsg;
	}

	public void setErrmsg(String errmsg) {
		this.errmsg = errmsg;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

}
